import { Injectable } from '@angular/core';
import * as _ from 'lodash';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MapLegendService {
  mapLegend = {};
  mapLegendList = [];
  private changeSource = new Subject();
  changeObservable = this.changeSource.asObservable();

  fetchMapLegendData(layerId: string) {
    return this.mapLegend[layerId].map(e => {
      return { name: e.styleName + e.styleValue, color: e.styleColor }
    })
  }

  remove(layerId: string) {
    delete this.mapLegend[layerId];
    _.remove(this.mapLegendList, (e) => {
      if (e.value === layerId) {
        return true;
      } else {
        return false;
      }
    })
    this.changeSource.next(layerId); // rxjs 发布模式
  }

  addMapLegend(value: any[], options) {
    this.mapLegend[value[0].layerId] = value;
    this.mapLegendList.push({ label: options.layerName, value: value[0].layerId });
  }

  removeAll() {
    this.mapLegend = {};
    this.mapLegendList = [];
  }

  constructor() { }
}
